package com.pinc.cpbl;

import java.io.InputStream;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class show_game extends WidgetActivity {
	private ListView lv;
	private SharedPreferences settings;
	private SharedPreferences.Editor editor;
	private int which_day;
	private ShowGameTask mTask;
	private int mLeague;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list_layout);
		mLeague = 0;

		switch (getIntent().getExtras().getInt("day"))
		{
		case 0:
			which_day = 0;
			break;
		case 2:
			which_day = 2;
			break;
		default:
			which_day = 1;
		}

		settings = PreferenceManager.getDefaultSharedPreferences(this);
		editor = settings.edit();

		initWidget();

		mTask = new ShowGameTask();
		mTask.execute(getApplicationContext());
	}

	@Override
	protected void initUpbar() {
		switch(which_day)
		{
		case 0:
			tv_title.setText(R.string.title_yesterday_game);
			break;
		case 2:
			tv_title.setText(R.string.title_tomorrow_game);
			break;
		default:
			tv_title.setText(R.string.title_today_game);
			break;
		}

		b_right.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				mTask = new ShowGameTask();
				mTask.execute(getApplicationContext());
				loadAD();
			}
		});

		b_right2.setVisibility(View.VISIBLE);
		b_right2.setText(getResources().getString(R.string.str_league2));
		b_right2.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				mLeague = 1-mLeague;
				mTask = new ShowGameTask();
				mTask.execute(getApplicationContext());
				loadAD();
			}
		});
	}

	@Override
	protected void initWidget() {
		super.initWidget();
		lv = (ListView)findViewById(R.id.listView1);
	}

	@Override
	protected void onPause() {
		super.onPause();
		mTask.cancel(true);
	}

	private class ShowGameTask extends AsyncTask<Context, Void, Integer> {
		private Game_Adapter g_adapter;

		@Override
		protected void onPreExecute() {
			b_right.setVisibility(View.INVISIBLE);
			b_right2.setVisibility(View.INVISIBLE);
			mProgbar.setVisibility(View.VISIBLE);
			lv.setAdapter(null);
		}

		protected Integer doInBackground(Context... ctx) {
			if (isNoInternet())
				return comn_param.ERR_ID_NO_NETWORK;
			try {
				g_adapter = new Game_Adapter(ctx[0]);
				InputStream is = MyHTTPHelper.ConnectURL(comn_param.CPBL_URL);
				if ( is == null)
					return comn_param.ERR_ID_TIMEOUT;
				Parser_Game mp = new Parser_Game(is);
				mp.stream.skip(8192);
				switch(which_day)
				{
				case 0:
					if ( mLeague == 1)
						mp.gotoTagByProp("span", "id", "lblG1_2");
					else
						mp.gotoTagByProp("span", "id", "lblG1");
					break;
				case 2:
					if ( mLeague == 1)
						mp.gotoTagByProp("span", "id", "lblG3_2");
					else
						mp.gotoTagByProp("span", "id", "lblG3");
					break;
				default:
					if ( mLeague == 1)
						mp.gotoTagByProp("span", "id", "lblG2_2");
					else
						mp.gotoTagByProp("span", "id", "lblG2");
					break;
				}
				mp.nextTag();
				mp.parse(g_adapter);
			} catch (Exception e) {
				e.printStackTrace();
				return comn_param.ERR_ID_UNKNOWN;
			}
			return comn_param.ID_SKIP;
		}

		@Override
		protected void onPostExecute(Integer err) {
			b_right.setVisibility(View.VISIBLE);
			b_right2.setVisibility(View.VISIBLE);
			if ( mLeague == 0)
				b_right2.setText(getResources().getString(R.string.str_league2));
			else
				b_right2.setText(getResources().getString(R.string.str_league1));
			mProgbar.setVisibility(View.INVISIBLE);

			showDialogCompat(err);

			lv.setAdapter(g_adapter);
			lv.setOnItemClickListener(new ItemClickListener(g_adapter));
		}
	}

	private class ItemClickListener implements OnItemClickListener {
		private Game_Adapter m_adapter;

		public ItemClickListener(Game_Adapter adapter) {
			m_adapter = adapter;
		}

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) {

			switch (m_adapter.getItem(position).status)
			{
			case Game_Info.ST_LIVE:
			case Game_Info.ST_OVER:
			case Game_Info.ST_LINEUP:
				Intent intent = new Intent(show_game.this, game_content.class);
				Bundle bd = new Bundle();
				bd.putInt("game_nr", (int)id);
				bd.putByte("game_status", m_adapter.getItem(position).status);
				bd.putInt("league", mLeague);
				intent.putExtras(bd);	
				startActivity(intent);
				break;
			default:
				if ( id > 0)
					showDialog((int)id);
				break;
			}
		}
	}

	@Override
	protected void onPrepareDialog(int id, Dialog dialog) {
		if ( id < 0)
			return;

		if ( settings.getInt(PreferenceHelper.BROADCAST_GAME_NR, 0) == id)
			((AlertDialog)dialog).setMessage(getResources().getString(R.string.str_ask_cancel_broadcast));
		else
			((AlertDialog)dialog).setMessage(getResources().getString(R.string.str_ask_broadcast));
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		final int game_id = id;
		AlertDialog.Builder builder = new AlertDialog.Builder(show_game.this);

		if ( id < 0)
		{
			return super.onCreateDialog(id);
		}
		else
		{
			builder.setMessage("").setCancelable(false)
			.setPositiveButton(getResources().getString(R.string.str_yes), new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int id) {
					if ( settings.getInt(PreferenceHelper.BROADCAST_GAME_NR, 0) == game_id)
						editor.putInt(PreferenceHelper.BROADCAST_GAME_NR, 0);
					else
						editor.putInt(PreferenceHelper.BROADCAST_GAME_NR, game_id);
					editor.commit();
					Intent i = new Intent(getApplicationContext(), BroadcastService.class);
					startService(i);
					dialog.cancel();
				}
			})
			.setNegativeButton(getResources().getString(R.string.str_no), new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int id) {
					dialog.cancel();
				}
			});
		}
		return builder.create();
	}
}
